﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace bjd
{
    public partial class goodsForm : Form
    {
        private GoodsBLL goodsBLL = null;
        private DataTable dtSource = null;

        public goodsForm()
        {
            InitializeComponent();
            goodsBLL = new GoodsBLL();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            addGoodsForm agf = new addGoodsForm();
            agf.Text = "添加商品信息";
            agf.ShowDialog();
            //刷新 重新绑定数据
            BindGoods();
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            /*
             删除选中的商品信息
              */
            int id = -1;
            try
            {
                if (dataGridView1.CurrentRow.Cells["id"].Value != null)
                    id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (id == -1)
            {
                MessageBox.Show("请选择要删除的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("你确定是否删除该记录？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (DialogResult.Yes == dr)
            {
                goodsBLL.Delete(id);
                //刷新 重新绑定数据
                BindGoods();
            }
        }

        private void modifyBtn_Click(object sender, EventArgs e)
        {
            /*
             修改商品信息，直接在datagridview双击修改，此功能无需实现
              */
            int id = -1;
            try
            {
                if (dataGridView1.CurrentRow.Cells["id"].Value != null)
                    id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (id == -1)
            {
                MessageBox.Show("请选择要修改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            addGoodsForm agf = new addGoodsForm();
            agf.id = id;
            agf.Text = "修改商品信息";
            agf.ShowDialog();
            //刷新 重新绑定数据
            BindGoods();
        }

        private void BindGoods()
        {
            dtSource = goodsBLL.GetGoodsList();
            if (dtSource != null)
            {
                dataGridView1.DataSource = dtSource;
                if (dataGridView1.Rows.Count == 0)
                {
                    delBtn.Enabled = false;
                    modifyBtn.Enabled = false;
                }
                else
                {
                    delBtn.Enabled = true;
                    modifyBtn.Enabled = true;
                }
            }
        }

        private void goodsForm_Load(object sender, EventArgs e)
        {
            BindGoods();
        }

        /// <summary>
        /// 绘画行号
        /// </summary>
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                DataGridView dataGridView = (DataGridView)sender;
                Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                    e.RowBounds.Location.Y,
                    dataGridView.RowHeadersWidth - 4,
                    e.RowBounds.Height);

                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                    dataGridView.RowHeadersDefaultCellStyle.Font,
                    rectangle,
                    dataGridView.RowHeadersDefaultCellStyle.ForeColor,
                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
            catch (Exception ex)
            {
                Console.Write("RowPostPaint：" + ex.Message);
            }
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text.Trim() == "")
            {
                BindGoods();
            }
            else
            {
                DataView dv = dtSource.DefaultView;
                dv.RowFilter = "name like '%" + txtSearch.Text.Trim() + "%'";
                dataGridView1.DataSource = dv;
            }
        }
    }
}
